package com.fmatos.amazingwallpapers.view.album;

import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.fmatos.amazingwallpapers.R;
import com.fmatos.amazingwallpapers.image.database.ImagesDataSource;
import com.fmatos.amazingwallpapers.image.database.dto.ImageRow;
import com.fmatos.amazingwallpapers.service.alarm.AlbumAlarm;
import com.fmatos.amazingwallpapers.service.image.BitmapServiceConnection;
import com.fmatos.amazingwallpapers.view.detail.ImageDetail;

public class AlbumActivity extends Activity {
	
	private static final String TAG = AlbumActivity.class.getSimpleName();
	
	/** Defines callbacks for service binding, passed to bindService() */
    private static BitmapServiceConnection mConnection;
    
	@Override
	protected void onStart() {
		super.onStart();
		mConnection = new BitmapServiceConnection(this);		
	}

	@Override
	protected void onStop() {
		super.onStop();
		// Unbind from the service
		if (mConnection != null && mConnection.isBound() ) {
			unbindService(mConnection);
			mConnection = null;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_album);
		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
		}
	}


	
//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.albums, menu);
//		return true;
//	}

//	@Override
//	public boolean onOptionsItemSelected(MenuItem item) {
//		// Handle action bar item clicks here. The action bar will
//		// automatically handle clicks on the Home/Up button, so long
//		// as you specify a parent activity in AndroidManifest.xml.
//		int id = item.getItemId();
//		if (id == R.id.action_settings) {
//			return true;
//		}
//		return super.onOptionsItemSelected(item);
//	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends ListFragment {

		private ImagesDataSource mDatasource;
		private String albumName;
		
		public PlaceholderFragment() {
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_album, container, false);
			
			prepareDB();
			
			Button mBtHitService = (Button) rootView.findViewById(R.id.btHitDownloadService);
			mBtHitService.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					ArrayAdapter<ImageRow> adapter = (ArrayAdapter<ImageRow>) getListAdapter();
					
			        if (mConnection != null && mConnection.isBound() && mConnection.getService() != null) {			        	
			        	mConnection.getService().getAlbumsInfo();
			        	mConnection.getService().getNewestImages(); // TODO make separate buttons
			        }
					
				}
			});
			
			Button mBtAlarm = (Button) rootView.findViewById(R.id.btStartAlarm);
			mBtAlarm.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					AlbumAlarm alarm = new AlbumAlarm(getActivity());
					alarm.setOneTimeAlarm();
				}
			});
				
			
			return rootView;
		}
		
		private void prepareDB() {
			
			albumName = getActivity().getIntent().getExtras().getString(ImageDetail.INTENT_EXTRA_ALBUM_ID);
			
			mDatasource = new ImagesDataSource(getActivity());
			mDatasource.open(); // this may take a long time, so better run it in the background
			List<ImageRow> values = mDatasource.getAlbumByName(albumName);

            Collections.reverse(values);
			// TODO use the SimpleCursorAdapter to show the
			// elements in a ListView
			ArrayAdapter<ImageRow> adapter = new ArrayAdapter<ImageRow>(getActivity(),
					android.R.layout.simple_list_item_1, values); 
			setListAdapter(adapter);
		}
	
		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			super.onListItemClick(l, v, position, id);
			
			ImageRow image = (ImageRow) getListAdapter().getItem(position);
			Toast.makeText(getActivity(), "Click item "  + image.getCaption(), Toast.LENGTH_SHORT).show();

			Intent intent = new Intent(getActivity(),ImageDetail.class);
			intent.putExtra(ImageDetail.INTENT_EXTRA_IMAGEROW, image.getUrl());
			intent.putExtra(ImageDetail.INTENT_EXTRA_ALBUM_ID, albumName);

			startActivity(intent);			
			
			
		}
		
	}

}
